package com.basic.business.common.utils.kmUtil;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.basic.business.common.constants.KmConstants;
import com.basic.business.common.utils.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class KmTagsUtils {

    /**
     * 快麦平台批量修改订单标签接口
     */
    public static final String TAG_METHOD = "erp.trade.tag.batch.update";


    /**
     * 正常处理标签
     */
    public static final String TAG_NORMAL_PROCESSING = "5103416699427508";

    /**
     * 不做处理标签
     */
    public static final String TAG_NO_PROCESSING = "5103419664997146";

    /**
     * 印刷品标签
     */
    public static final String TAG_PRINTED = "4936488555782405";


    private static final Map<String, String> TAG_ID_TO_NAME = new ConcurrentHashMap<>();

    static {
        TAG_ID_TO_NAME.put(TAG_NORMAL_PROCESSING, "正常处理");
        TAG_ID_TO_NAME.put(TAG_PRINTED, "印刷品");
        TAG_ID_TO_NAME.put(TAG_NO_PROCESSING, "不做处理");
    }

    /**
     * 更新标签
     * @param mainSid 主订单系统单号
     * @param tags 订单标签
     * @param targetTag 目标标签
     */
    public static String updateExclusiveTag(long mainSid, String tags, String targetTag) {
        StringBuilder logContent = new StringBuilder("标签处理：");

        if (StringUtils.isNotBlank(tags)) {
            Set<String> tagSet = new HashSet<>(Arrays.asList(tags.split(",")));

            // 1. 检查并删除不需要的标签
            if (tagSet.contains(TAG_NORMAL_PROCESSING) && !targetTag.equals(TAG_NORMAL_PROCESSING)) {
                String result = tagProcessed(mainSid, TAG_NORMAL_PROCESSING, 2);
                logContent.append(StringUtils.format("删除【正常处理】标签{}；", result));
            }
            if (tagSet.contains(TAG_PRINTED) && !targetTag.equals(TAG_PRINTED)) {
                String result = tagProcessed(mainSid, TAG_PRINTED, 2);
                logContent.append(StringUtils.format("删除【印刷品】标签{}；", result));
            }
            if (tagSet.contains(TAG_NO_PROCESSING) && !targetTag.equals(TAG_NO_PROCESSING)) {
                String result = tagProcessed(mainSid, TAG_NO_PROCESSING, 2);
                logContent.append(StringUtils.format("删除【不做处理】标签{}；", result));
            }

            // 2. 检查是否需要添加目标标签
            if (!tagSet.contains(targetTag)) {
                String result = tagProcessed(mainSid, targetTag, 1);
                logContent.append(StringUtils.format("新增【{}】标签{}；", TAG_ID_TO_NAME.get(targetTag), result));
            }
        } else {
            // tags为空时直接添加目标标签
            String result = tagProcessed(mainSid, targetTag, 1);
            logContent.append(StringUtils.format("新增【{}】标签{}；", TAG_ID_TO_NAME.get(targetTag), result));
        }

        return logContent.length() > 5 ? logContent.toString() : "";
    }


    /**
     * 标签处理
     * @param sid 系统订单号
     * @param tagIds 标签id
     * @param type 标签类型 1新增 2删除
     */
    private static String tagProcessed(long sid, String tagIds, int type) {
        Map<String,String> params = new ConcurrentHashMap<>();
        params.put("sids", String.valueOf(sid));
        params.put("tagIds", tagIds);
        params.put("type", String.valueOf(type));
        String result = HttpUtil.post(KmConstants.URL, JSON.toJSONString(PostParamsUtil.getTagParams(TAG_METHOD, params)));
        JSONObject jsonResult = JSONObject.parseObject(result);
        boolean success = jsonResult.getBoolean("success");
        return success ? "成功" : "失败";
    }
}
